// languages.cpp
// Customizer_Pro
// Protea Customizer Pro / Business Logic for managing languages
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "pro_customizer.h"




//@ SEC 3
//@ CHA Language Defn Add
//@ COD LanguageDefnAdd
//@ TAB LANGUAGE_DEFN Write
//@ GEN Use LanguageDefnAdd to add a language defn record
void LanguageDefnAdd( XCBuffer* lang_defn, XCTransactionVars* vars ) {
	TRANS_INFUNC( LanguageDefnAdd );

	long last_number = -1;
	XCDBStatement* stmt = NULL;
	XCBuffer* temp = NULL;

	// Determine the next available language number
	temp = App->CreateBuffer( FMT_LANGUAGE_DEFN );
	try {
		XCOrder ord;
		XCCompare comp;
		bool found = false;

		vars->query->MakeSELECT( temp );
		vars->query->AddORDERBY( FLD_LANG_ID, &ord );
		stmt = vars->dbtxn->ExecuteSQL( vars->query );
		while ( !found && !stmt->Eof() )
		{
			long value = temp->GetField( FLD_LANG_ID )->GetLong( true );
			if ( value != last_number + 1 )
			{
				// we found a blank space
				found = true;
				last_number++;
			}
			else
			{
				last_number = value;
				stmt->MoveNext();
			};
		};
		// Cleanup now
		App->CleanupObjects( 2, &temp, &stmt );

		if (!found)
			last_number++;
		lang_defn->GetField( FLD_LANG_ID )->SetLong( last_number );

	} catch( ... ) {
		App->CleanupObjects( 2, &temp, &stmt );
		throw;
	};

	// Insert the language record
	vars->query->MakeFull( SQL_STMT_INSERT, lang_defn );
	vars->dbtxn->ExecuteSQL( vars->query, SQL_NO_RESULT );

	TRANS_OUTFUNC();
};



//@ SEC 3
//@ CHA Language Defn Del
//@ COD LanguageDefnDel
//@ TAB LANGUAGE_DEFN Write
//@ GEN Use LanguageDefnAdd to add a language defn record
void LanguageDefnDel( XCBuffer* lang_defn, XCTransactionVars* vars ) {
	TRANS_INFUNC( LanguageDefnDel );

	// Delete the LANGUAGE_DEFN record
	vars->query->MakeFull( SQL_STMT_DELETE, lang_defn );
	vars->dbtxn->ExecuteSQL( vars->query, SQL_NO_RESULT );

	// Delete all messages with this language number

	// Delete all Literal values with this language number

	TRANS_OUTFUNC();
};



//@ SEC 2
//@ CHA Add Language
//@ COD AddLanguage
TRANSACTION_CODE( AddLanguage ) {
	XCBuffer* lang_defn = NULL;

	//@ GRS Start the transaction
	START_TXN( AddLanguage );

		//@ Check for blank LITERAL_NAME
		XCField* lang_name = txnvars.buffer->GetField( FLD_LANG_NAME );
		if (lang_name->IsBlank())
			txnvars.Errors->Add( ERR_IS_BLANK, SEV_CRITICAL, FLD_LANG_NAME, 1, lang_name );

		//@ Create Language Defn buffer
		lang_defn = App->CreateBuffer( FMT_LANGUAGE_DEFN );

		//@ Move screen info to transaction buffer
		lang_defn->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();

			//@ SUB LanguageDefnVerify to verify that application can be added
			LanguageDefnVerify( lang_defn, &txnvars, VERIFY_ADD );

			//@ Check for errors
			txnvars.Errors->Check();

			//@ SUB LanguageDefnAdd to add the new application
			LanguageDefnAdd( lang_defn, &txnvars );

			LOG_TRANSACTION();

		END_DB_TXN();

		txnvars.buffer->MoveCorresponding( lang_defn, txnvars.Errors );
		App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

	CLEANUP_TXN();
		App->CleanupObjects( 1, &lang_defn );
	END_TXN();
	//@ GRE End the transaction
};




//@ SEC 2
//@ CHA Del Language
//@ COD DelLanguage
TRANSACTION_CODE( DelLanguage ) {
	XCBuffer* TransBuf = NULL;

	//@ GRS Start the transaction
	START_TXN( DelLanguage );

		//@ Create literal buffer
		TransBuf = App->CreateBuffer( FMT_LANGUAGE_DEFN, false );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();
			//@ SUB LiteralDefnVerify to verify that record can be deleted
			LanguageDefnVerify( TransBuf, &txnvars, VERIFY_DELETE );

			txnvars.Errors->Check();

			//@ SUB LanguageDefnDel to delete the record.
			LanguageDefnDel( TransBuf, &txnvars );

			LOG_TRANSACTION();

		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 1, &TransBuf );
	END_TXN();
	//@ GRE End the transaction
};
